home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / Documentation / Tech Notes & Articles / Recipes / Edit Menu / Undo Recipes < prev   
Encoding:
Text File  |  1995-07-12  |  4.9 KB  |  93 lines  |  [TEXT/ttxt]

  1. Undo Recipes
  2. By The OpenDoc Design Team
  3. 14 April, 1994
  4.  
  5.  
  6. © 1993-1995  Apple Computer, Inc. All Rights Reserved.
  7. Apple, the Apple logo, and Macintosh are registered trademarks of Apple Computer, Inc.
  8. Mac and OpenDoc are trademarks of Apple Computer, Inc.
  9.  
  10.  
  11. Introduction
  12.  
  13. Undo under OpenDoc is multi-level and system-wide. 
  14.  
  15. Please note that the ODPart API calls, ReadActionState and WriteActionState are not supported by Undo in OpenDoc 1.0 on the Mac.
  16.  
  17. Parts
  18.  
  19. Parts should add undoable actions to the undo history using AddActionToHistory. whichPart identifies your part object, actionData is a reference to your data (a magic cookie), actionType is decribed below. undoActionLabel and redoActionLabel are user-visible strings corresponding to the undo and redo menu items.
  20.  
  21.    void AddActionToHistory(in ODPart whichPart,
  22.          in ODActionData actionData,
  23.          in ODActionType actionType,
  24.          in ODName undoActionLabel,
  25.          in ODName redoActionLabel);
  26.  
  27. Here’s some sample code:
  28.  
  29.  // CREATE UNDO MENU ITEM TEXT
  30.  ODIText* undoActionName = CreateIText("Undo My Action");
  31.  ODIText* redoActionName = CreateIText("Redo My Action");
  32.  
  33.  // CREATE A STRUCTURE TO HOLD DATA ABOUT ACTION
  34.  MyUndoInfo* uInfo = (MyUndoInfo*)AllocMem(sizeof(MyUndoInfo));
  35.  // ADD DATA TO STRUCTURE (TASK SPECIFIC)
  36.  uInfo->someData = …
  37.  
  38.  MyGetSession()->GetUndo()->AddActionToHistory(_fPartWrapper,
  39.                                            (ODActionData)uInfo,
  40.                                            kODSingleAction,
  41.                                            undoMenuText,
  42.                                            redoMenuText);
  43.  
  44.  DeleteIText(undoActionName );
  45.  DeleteIText(redoActionName );
  46.  
  47. The above code illustrates adding a single action to the undo history. Undo makes a copy of the ODActionData and stores it away. To add a transaction to the undo history, such as the drag-moving of some data from one part to a another, parts must use the ODActionTypes, kODBeginAction and kODEndAction.
  48.  
  49. NOTE: Change in recipe for transactions: The part initiating an action that may span multiple parts should place an action on the stack using the kODBeginAction constant for the ODActionType parameter to AddActionToHistory. When the action is complete, that same part should place the “end” action on the stack using the kODEndAction constant. In between these times, other parts may add single actions to the action history. These actions become part of the entire transaction.
  50.  
  51. For example, in the case of drag-moving of some data from one part to a another, the source part should add a begin action to the action history, the receiving part should add item(s) to the undo stack about the data received using the kODSingleAction constant, and finally, the source part can close out the transaction  by adding an end action to the history.
  52.  
  53. Another example: in the case of a paste with a link, the destination part should add the begin and end actions to the stack  and the source part should add one or more single actions to the undo history.
  54.  
  55. If the part placing the begin and end actions on the stack notices an error at any point after placing the begin action onto the stack, it should roll back the transaction. Currently there is no clean way to do this other than to clear the entire undohistory. We will mostly likely add a new functionality in the near future to allow for rolling back a transaction.
  56.  
  57. When an action is undone, ODPart::UndoAction is called for the part that furnished the action. Similarly ODPart::RedoAction is called when an action is redone. When a transaction is undone or redone, every action between the beginning and ending actions, as well as those actions themselves, will be undone or redone.
  58.  
  59. ODPart::DisposeActionState is called whenever an item on the undo or redo stack is permanently removed.  You will be passed a copy of the ODActionData that you originally added with AddActionToHistory. You must take whatever action is relevant to your part. This may be the disposal of any auxiliary data associated with this undo action.
  60.  
  61. Container applications
  62.  
  63. Container applications should manage the Undo and Redo menu items and should call Undo and Redo when they are selected. Here's some sample code for the display and activation of these menu items:
  64.  
  65.  ODUndo*         undo = MyGetSession()->GetUndo(ev);
  66.  ODPart*         part;
  67.  ODActionData    actionData;
  68.  ODActionType    actionType;
  69.  ODName*         actionLabel;
  70.  
  71.  // CREATE EMPTY ITEXT STRUCTURE TO BE COPIED INTO
  72.  actionLabel = CreateIText(UCHAR_MAX);
  73.  
  74.  // LOOK AT THE STACK
  75.  if (undo->PeekUndoHistory(ev, &part, &actionData, &actionType,
  76.         actionLabel))
  77.  {
  78.   fMenuBar->EnableCommand(ev, kODCommandUndo, kODTrue);
  79.   fMenuBar->SetItemString(ev, kODCommandUndo, actionLabel);
  80.  }
  81.  else
  82.  {
  83.   fMenuBar->EnableCommand(ev, kODCommandUndo, kODFalse);
  84.   this->ResetUndoText(editMenu);
  85.  }
  86.  
  87.  DeleteIText(actionLabel);
  88.  
  89.  // SIMILARLY FOR REDO
  90.  …
  91.  
  92. ResetUndoText is a function that sets the text of the Undo menu item to the default string.
  93.